package dao.impl;

import dao.BaseDAO;
import dao.GoodsInStoreDAO;
import entity.GoodsInStore;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

/**
 * Created by asus on 2017/6/22.
 */
@Repository("goodsInStoreDAO")
public class GoodsInStoreDAOImpl extends BaseDAO implements GoodsInStoreDAO {

    /**
     * GoodsInStore的GoodsInStoreMapper映射类
     */
    private class GoodsInStoreMapper implements RowMapper<GoodsInStore> {
        @Override
        public GoodsInStore mapRow(ResultSet resultSet, int i) throws SQLException {
            GoodsInStore goodsInStore = new GoodsInStore();
            goodsInStore.setId(resultSet.getLong("id"));
            goodsInStore.setGoodsId(resultSet.getLong("goods_id"));
            goodsInStore.setStoreId(resultSet.getLong("store_id"));
            goodsInStore.setInQuantity(resultSet.getInt("in_quantity"));
            goodsInStore.setInDate(resultSet.getDate("in_date"));
            return goodsInStore;
        }
    }

    @Override
    public Integer getCount() {
        String sql="select count(*) from goods_in_store";
        try{
            return jdbcTemplate.queryForObject(sql,Integer.class);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<GoodsInStore> getPage(int page,int size) {
        System.out.println("进入了dao");
        if (page < 1) {
            page = 1;
        }
        if (size < 0) {
            size = 20;
        }
        String sql = "select * from goods_in_store limit ?,?";
        int skip = (page - 1) * size;
        try {
            return jdbcTemplate.query(sql, new Object[]{skip, size}, new GoodsInStoreMapper());
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Long save(final GoodsInStore goodsInStore) {
        final String sql="insert into goods_in_store(goods_id,store_id,in_quantity,in_date) values(?,?,?,?)";
        KeyHolder keyHolder=new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps=connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                //role 被内部类使用，必须声明为final
                ps.setLong(1,goodsInStore.getGoodsId());
                ps.setLong(2,goodsInStore.getStoreId());
                ps.setInt(3,goodsInStore.getInQuantity());
                ps.setDate(4, (Date) goodsInStore.getInDate());
                return ps;
            }
        },keyHolder);
        //获取id
        return (keyHolder.getKey().longValue());
    }
}
